﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBORpt = Texfina.BORpt.Rh;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using CrystalDecisions.Shared;

namespace RH
{
    public partial class wfRemBasicaRpt : EditPage
   {


      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();
         
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = FormEditAction.Edit ;
         this.SetInitialTitle("Listado de Remuneración Básica");
      }

      public override void LoadListControls()
      {
          ETablaGenDet objEDet = new ETablaGenDet();
          ECCosto objECCosto = new ECCosto();
          EEmpEstable objEEmp = new EEmpEstable();
          SYBOQry.IEmpresa objBOEmp = (SYBOQry.IEmpresa)WCFHelper.GetObject<SYBOQry.IEmpresa>(typeof(SYBOQry.Empresa));
          IBOQuery objBOCCosto = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.CCosto));
          MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

          EUNegocio objEUNeg = new EUNegocio();

          IBOQuery objBOUNego = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.UNegocio));

          //objEDet.IdTabla = ConstMultitabla.MES;
          //DataTable dt = objBOI.GetDetailByList(objEDet);
          //WebHelper.LoadListControl(ddlidMes, dt, "dsDescrip", "idCodigo", ConstCustomFirstListItem.ITEM_ALL);

          //objEDet.IdTabla = ConstMultitabla.FORMA_PAGO;
          //dt = objBOI.GetDetailByList(objEDet);
          //WebHelper.LoadListControl(ddlidFormaPago, dt, "dsDescrip", "idCodigo", ConstCustomFirstListItem.ITEM_ALL);
          DataTable dt = new DataTable();
          objEEmp.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          dt = objBOEmp.GetEstablecimientos(objEEmp);
          WebHelper.LoadListControl(ddlidEstable, dt, "dsEstable", "idEstable", ConstCustomFirstListItem.ITEM_ALL);

          objECCosto.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          dt = objBOCCosto.GetList(objECCosto);
          WebHelper.LoadListControl(ddlidCCosto, dt, "dsCCosto", "idCCosto", ConstCustomFirstListItem.ITEM_ALL);

          objEUNeg.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          dt = objBOUNego.GetList(objEUNeg);
          WebHelper.LoadListControl(ddlidUNegocio, dt, "dsUNegocio", "idUNegocio", ConstCustomFirstListItem.ITEM_ALL);

          objEDet.IdTabla = ConstMultitabla.TIPO_COLABORADOR;
          dt = objBOI.GetDetailByList(objEDet);
          WebHelper.LoadListControl(ddlidTipoTrab, dt, "dsDescrip", "idCodigo", ConstCustomFirstListItem.ITEM_ALL);
      }

      public override void DataToControls()
      {

      
      }

      public override void RegisterGeneralScripts()
      {
         
         StringBuilder sb = new StringBuilder();

         //sb.AppendLine("var varRet = modalDialog('wfPersonal.aspx',725,400);");
         //sb.AppendLine("if(varRet==null)");
         //sb.AppendLine("return false;");
         //sb.AppendLine("ctl = document.getElementById('" + this.hdnidPersonal.ClientID + "');");
         //sb.AppendLine("ctl.value = varRet[0];");
         ////sb.AppendLine("ctl = document.getElementById('" + this.txtidPersonal.ClientID + "');");
         ////sb.AppendLine("ctl.value = varRet[1];");
         ////this.txtidPersonal.OnClientClick = sb.ToString();

         //sb = new StringBuilder();

         //sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidMes.ClientID + "','Seleccione el Mes.');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");
         //sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidFormaPago.ClientID + "','Seleccione la forma de Pago.');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");
         //sb.AppendLine("var varRet = modalDialog('wfProcPlani.aspx?IdMes=' + document.getElementById('" + this.ddlidMes.ClientID + "').value + '&IdFormaPago=' + document.getElementById('" + this.ddlidFormaPago.ClientID + "').value,725,400);");
         //sb.AppendLine("if(varRet==null)");
         //sb.AppendLine("return false;");
         //sb.AppendLine("ctl = document.getElementById('" + this.hdnidPlanilla.ClientID + "');");
         //sb.AppendLine("ctl.value = varRet[0];");
         //sb.AppendLine("ctl = document.getElementById('" + this.hdnnuProcAnual.ClientID + "');");
         //sb.AppendLine("ctl.value = varRet[4];");
         //sb.AppendLine("ctl = document.getElementById('" + this.txtidPlanilla.ClientID + "');");
         //sb.AppendLine("ctl.value = varRet[3] + ' - ' + varRet[2] + ' - ' + varRet[1] + ' - ' + varRet[4];");
         //this.txtidPlanilla.OnClientClick = sb.ToString();

         sb = new StringBuilder();

         //sb.AppendLine("ctl = document.getElementById('" + this.txtidPersonal.ClientID + "');");
         //sb.AppendLine("ctl.value = '';");
         sb.AppendLine("ctl = document.getElementById('" + this.ddlidEstable.ClientID + "');");
         sb.AppendLine("ctl.value = '';");
         this.bttClear.OnClientClick = sb.ToString();

         sb = new StringBuilder();

         sb.AppendLine("function validateEditForm(key){");
         //sb.AppendLine("      var varRpta = isEmptyControl('" + this.txtidPlanilla.ClientID + "','Seleccione la Planilla');");
         //sb.AppendLine("      if(varRpta==true)");
         //sb.AppendLine("      return !varRpta;");
         sb.AppendLine("__doPostBack('" + this.mnuToolbar.UniqueID + "',key);");
         sb.AppendLine("}");

  
         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);
      }

      public void RegisterEditPopupScripts()
      {

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_PRINT:
               PrintReport("");
               break;
            case ConstMenuToolbarValue.BUTTON_EXPORT:
               ExportReport("");
               break;
         }

      }

      public override void PrintReport(string key)
      {
         ReportDocument objRpt = new ReportDocument();

         DataSet ds = (DataSet)this.GetDataReport(key);

         if (ds.Tables[0].Rows.Count == 0)
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage("ATENCIÖN", "No se encotraron Resultados", TypeMessageAlert.atention, false, "");
            return;
         }

         objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/RemuneracionBasica.rpt"));
         objRpt.SetDataSource(ds.Tables[0]);


         MemoryStream ms = new MemoryStream();

         Response.Clear();

         ms = (MemoryStream)objRpt.ExportToStream(ExportFormatType.PortableDocFormat);
         objRpt.Dispose();
         objRpt = null;

         Response.AddHeader("Content-Disposition", "attachment;filename=RemuneracionBasica.pdf");
         Response.Buffer = true;
         Response.ContentType = "application/pdf";

         ms.WriteTo(Response.OutputStream);
         Response.Flush();
         Response.End();

      }
      
      public override void ExportReport(string key)
      {
         DataSet ds = (DataSet)this.GetDataReport(key);
         if (ds.Tables[0].Rows.Count == 0)
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage("ATENCIÓN", "No se encotraron Resultados", TypeMessageAlert.atention, false, "");
            return;
         }
         ExcelHelper.ToExcel(ds, "Data.xls", Response);

      }

      public override object GetDataReport(string key)
      {

          EPerRemuBandeja objE = new EPerRemuBandeja();
         RHBORpt.IPersonal objBO = (RHBORpt.IPersonal)WCFHelper.GetObject<RHBORpt.IPersonal>(typeof(RHBORpt.Personal));

         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

         if (this.ddlidCCosto.SelectedItem.Value == "")
             objE.IdCCosto = null;
         else
             objE.IdCCosto = this.ddlidCCosto.SelectedItem.Value;

         if (this.ddlidUNegocio.SelectedItem.Value == "")
             objE.IdUNegocio = null;
         else
             objE.IdUNegocio = this.ddlidUNegocio.SelectedItem.Value;

         if (this.ddlidEstable.SelectedItem.Value == "")
             objE.IdEstable = null;
         else
             objE.IdEstable = this.ddlidEstable.SelectedItem.Value;

         if (this.ddlidTipoTrab.SelectedItem.Value == "")
             objE.IdTipoTrab = null;
         else
             objE.IdTipoTrab = this.ddlidTipoTrab.SelectedItem.Value;

         if (this.txtfeIngplaA.Text != "")
             objE.feIngplaA = Convert.ToDateTime(this.txtfeIngplaA.Text);
         else
             objE.feIngplaA = null;

         if (this.txtfeIngplaB.Text != "")
             objE.feIngplaB = Convert.ToDateTime(txtfeIngplaB.Text);
         else
             objE.feIngplaB = null;

         if (rbtEstadoCesado.Checked)
         {
            objE.StContrato = ConstFlagEstado.ACTIVADO;
         }
         if (rbtEstadoNoCesado.Checked)
         {
            objE.StContrato = ConstFlagEstado.DESACTIVADO;
         }
         DataTable dt = objBO.GetByCriteriaPerRemu(objE);
 
         DataSet ds = new DataSet();
         ds.Tables.Add(dt);


         return ds;


      }

  
   }
}